<template>
    <div id="city">
      <Header ></Header>
      <Search :cities="cities"></Search>
      <List
        :hotCities="hotCities"
        :cities="cities"
        :letter="letter"
      ></List>
      <Alphabet :cities="cities" @change="handleLetterChange"></Alphabet>
    </div>
</template>

<script>
import Header from './components/Header'
import Search from './components/Search'
import List from './components/List'
import Alphabet from './components/Alphabet'
export default {
  name: 'City',
  components: {
    Header,
    Search,
    List,
    Alphabet
  },
  data () {
    return {
      hotCities: [],
      cities: {},
      letter: ''
    }
  },
  methods: {
    getCityInfo () {
      this.$http.get('/api/city.json')
        .then(this.handleCityInfoSucc)
        .catch(this.handleCityInfoFail)
    },
    handleCityInfoSucc (res) {
      // console.log(res.data)
      const ret = res.data // 1.获取整个数据对象res.data{}
      if (ret.ret && ret.data) { // 2.查看原始数据中的ret = true || data是否有数据
        const data = ret.data // 3.此时data是JSON数据中的data
        this.hotCities = data.hotCities
        this.cities = data.cities
      }
    },
    handleCityInfoFail (err) {
      alert(err.message)
    },
    handleLetterChange (val) {
      // console.log(val)
      this.letter = val
    }
  },
  mounted () {
    this.getCityInfo()
    // console.log(this.$route)
    // this.city = this.$route.query.ct
  }
}
</script>

<style lang="stylus" scoped>

</style>
